﻿/* 
* Copyright (c) 2010, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Bio.Data.Interfaces.Drawing;

namespace Bio.Data.Providers.Structure
{
    public class RNASecondaryStructureNucleotideTextShape : ITextShape
    {
        public static RNASecondaryStructureNucleotideTextShape FromRNAML(IEnumerable<XComment> formattingcomments)
        {
            RNASecondaryStructureNucleotideTextShape retValue = null;
            foreach (var formattingcomment in formattingcomments)
            {
                if (!string.IsNullOrEmpty(formattingcomment.Value) && formattingcomment.Value.StartsWith(RNAMLTags.StrMLExtension))
                {
                    string[] fields = Regex.Split(formattingcomment.Value, "::");
                    if (fields.Length == 3 && fields[0].Equals(RNAMLTags.StrMLExtension))
                    {
                        if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelNucleotideFontLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureNucleotideTextShape();

                            retValue.Font = fields[2];
                        }
                        else if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelNucleotideFillLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureNucleotideTextShape();

                            retValue.Color = fields[2];
                        }
                        else if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelNucleotideFontStyleLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureNucleotideTextShape();

                            retValue.FontStyle = fields[2];
                        }
                        else if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelNucleotideFontWeightLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureNucleotideTextShape();

                            retValue.FontWeight = fields[2];
                        }
                        else if (fields[1].Equals(RNAStructureModelXMLTags.StructureModelNucleotideFontSizeLabel))
                        {
                            if (retValue == null)
                                retValue = new RNASecondaryStructureNucleotideTextShape();

                            double fontSize;
                            if (double.TryParse(fields[2], out fontSize))
                            {
                                if (fontSize > 0.0)
                                    retValue.FontSize = fontSize;
                            }
                        }
                    }
                }
            }
            return retValue;
        }

        public static RNASecondaryStructureNucleotideTextShape FromXML(XElement props)
        {
            if (props != null &&
                !props.IsEmpty /*&&
                props.Name.LocalName.Equals(RNAStructureModelXMLTags.StructureModelNucleotideLabel)*/)
            {
                RNASecondaryStructureNucleotideTextShape retValue = null;
                XElement nucleotideFillElement = props.Element(RNAStructureModelXMLTags.StructureModelNucleotideFillLabel);
                XElement nucleotideFontElement = props.Element(RNAStructureModelXMLTags.StructureModelNucleotideFontLabel);
                XElement nucleotideFontSizeElement = props.Element(RNAStructureModelXMLTags.StructureModelNucleotideFontSizeLabel);
                XElement nucleotideFontStyleElement = props.Element(RNAStructureModelXMLTags.StructureModelNucleotideFontStyleLabel);
                XElement nucleotideFontWeightElement = props.Element(RNAStructureModelXMLTags.StructureModelNucleotideFontWeightLabel);

                if (nucleotideFillElement != null &&
                    !nucleotideFillElement.IsEmpty)
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureNucleotideTextShape();

                    retValue.Color = nucleotideFillElement.Value;
                }

                if (nucleotideFontElement != null &&
                    !nucleotideFontElement.IsEmpty)
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureNucleotideTextShape();

                    retValue.Font = nucleotideFontElement.Value;
                }

                if (nucleotideFontStyleElement != null &&
                    !nucleotideFontStyleElement.IsEmpty)
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureNucleotideTextShape();

                    retValue.FontStyle = nucleotideFontStyleElement.Value;
                }

                if (nucleotideFontWeightElement != null &&
                    !nucleotideFontWeightElement.IsEmpty)
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureNucleotideTextShape();

                    retValue.FontWeight = nucleotideFontWeightElement.Value;
                }

                double fontSize;
                if (nucleotideFontSizeElement != null &&
                    !nucleotideFontSizeElement.IsEmpty &&
                    Double.TryParse(nucleotideFontSizeElement.Value, out fontSize) &&
                    fontSize > 0.0)
                {
                    if (retValue == null)
                        retValue = new RNASecondaryStructureNucleotideTextShape();

                    retValue.FontSize = fontSize;
                }

                return retValue;
            }
            else { return null; }
        }

        #region ITextShape Members

        public string Font { get; set; }
        public double FontSize { get; set; }
        public string FontStyle { get; set; }
        public string FontWeight { get; set; }
        public string Color { get; set; }

        #endregion
    }
}
